import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";
interface AuthState {
  token: string | null;
  user: any | null;
  isInitialized: boolean;
  setInitialized: () => void;
  setAuth: (token: string, user: any) => void;
  logout: () => void;
}

export const useAuthStore = create<AuthState>()(
  persist(
    (set) => ({
      token: null,
      user: null,
      isInitialized: true,
      setAuth: (token: any, user: any) => {
        set({ token, user, isInitialized: true });
      },
      logout: () => set({ token: null, user: null, isInitialized: true }),
    }),
    {
      name: "auth-storage",
      storage: createJSONStorage(() => localStorage),
      skipHydration: true,
      partialize: (state: any) => ({ token: state.token, user: state.user }),
    }
  )
);
